﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WinServiceKeepAlive
{
    public class ServiceHelper
    {
        /// <summary>
        /// 判断是否安装了某个服务
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>

        public static bool ISWindowsServiceInstalled(string serviceName)
        {
            try
            {
                using (var service = new ServiceController(serviceName))
                {
                    return true;
                }
            }
            catch (InvalidOperationException) // 服务不存在
            {
                return false;
            }
            catch (Exception) // 其他异常如权限不足等
            {
                return false;
            }
        }


        /// <summary>
        /// 启动某个服务
        /// </summary>
        /// <param name="serviceName"></param>

        public static bool StartService(string serviceName)
        {
            try
            {
                using (var service = new ServiceController(serviceName))
                {
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                    if ((service.Status == ServiceControllerStatus.Stopped)
                            || (service.Status == ServiceControllerStatus.StopPending))
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            catch (InvalidOperationException) // 服务不存在
            {
                return false;
            }
            catch (Exception) // 其他异常如权限不足等
            {
                return false;
            }
        }

        /// <summary>
        /// 停止某个服务
        /// </summary>
        /// <param name="serviceName"></param>
        public static bool StopService(string serviceName)
        {
            try
            {
                using (var service = new ServiceController(serviceName))
                {
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                    if ((service.Status == ServiceControllerStatus.Stopped)
                            || (service.Status == ServiceControllerStatus.StopPending))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (InvalidOperationException) // 服务不存在
            {
                return false;
            }
            catch (Exception) // 其他异常如权限不足等
            {
                return false;
            }
        }


        /// <summary>
        /// 判断某个服务是否启动
        /// </summary>
        /// <param name="serviceName"></param>

        public static bool ISStart(string serviceName)
        {
            try
            {
                using (var service = new ServiceController(serviceName))
                {
                    if ((service.Status == ServiceControllerStatus.Stopped)
                            || (service.Status == ServiceControllerStatus.StopPending))
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            catch (InvalidOperationException) // 服务不存在
            {
                return false;
            }
            catch (Exception) // 其他异常如权限不足等
            {
                return false;
            }
        }
    }
}
